// pages/Conveinfo/index/index.js
var app = getApp();
var ikz = require('../../Common/tools.js');
var arrs = [];
var start_clientX;
var end_clientX;
const util = require("../../../utils/utils.js")


Page({

  /**
   * 页面的初始数据
   */
  data: {
    banner_info: '',//banner图信息
    conven_type_info: '',//所有分类信息
    all_browse: '',//总浏览量
    all_release: '',//总发布数
    conven_info: '',//当前显示信息
    page: 1,//页数
    limit: 6,//每页条数
    conven_top_info: '',//优选信息
    shop_info: '',//商家信息
    bg_today: '#0AC4BA',//今日信息背景色
    co_today: '',//今日信息字体色
    bg_yesterday: '',//昨日信息背景色
    co_yesterday: '',//昨日信息字体色
    bg_all: '',//全部信息背景色
    co_all: '',//全部信息字体色
    datyp: '',//按时间分类
    prodis: 'none',//底部提示是否显示
    imgnum: 1,//banner第几张
    indicatorDots: true,
    scrollLeft: 20,
    windowWidth: wx.getSystemInfoSync().windowWidth,
    dis: 'none',
    display: 'none',

    userInfo: '',//用户信息
    disbtn: '',//退出按钮是否显示
    islog: 2,//2可以登录，3不可点击登录
  },



  //登录
  bindViewTap: function () {
    this.setData({
      islog: 3,
    })
    ikz.login('', (res) => {
      console.log(res.data)
      //登录成功后的业务逻辑
      wx.setStorageSync('local_user_session', res.data.trd_session);
      app.globalData.openUserInfo = res.data.trd_session;
      app.globalData.user_openinfo = res.data.user_info;
      wx.showToast({
        title: '已登录成功',
        duration: 2000
      });
      this.setData({
        userInfo: app.globalData.user_openinfo,
        disbtn: 'block',
      })
    });

  },
  //退出登陆，清除缓存 用户信息
  clearInfo: function () {
    ikz.loginOut();
    this.setData({
      userInfo: '',
      disbtn: 'none',
      islog: 2,
    })
  },
  //跳转到余额页
  myBanlance: function (e) {
    ikz.nav('../../Personal/balance/balance', 'n');
  },
  //跳转到我的收藏页
  myCollect: function (e) {
    ikz.nav('../../Personal/collect/collect', 'n');
  },
  //跳转到我的发布页
  myRelease: function (e) {
    ikz.nav('../../Personal/release/release', 'n');
  },
  //跳转到我的订单页
  myOrder: function (e) {
    ikz.nav('../../Personal/myorder/myorder', 'n');
  },
  //跳转到客服页
  myService: function (e) {
    ikz.nav('../../Personal/service/service', 'n');
  },











  // 滑动开始  
  touchstart: function (e) {
    start_clientX = e.changedTouches[0].clientX
  },
  // 滑动结束  
  touchend: function (e) {
    end_clientX = e.changedTouches[0].clientX;
    if (end_clientX - start_clientX > 120) {
      this.setData({
        display: "block",
        translate: 'transform: translateX(' + this.data.windowWidth * 0.7 + 'px);',
        dis: 'block'
      })
    } else if (start_clientX - end_clientX > 0) {
      this.setData({
        display: "none",
        translate: '',
        dis: 'none'
      })
    }
  },
  // 头像  
  showview: function (e) {
    // start_clientX = e.changedTouches[0].clientX,
    this.setData({
      display: "block",
      translate: 'transform: translateX(' + this.data.windowWidth * 0.7 + 'px);',
      dis: 'block'
    })
  },
  // 遮拦  
  hideview: function () {
    this.setData({
      display: "none",
      dis: "none",
      translate: '',
    })
  },










  //banner变化
  swiperChange: function (e) {
    var imgnum = e.detail.current + 1;
    this.setData({
      imgnum: imgnum,
    });
  },
  //跳转到图片页
  toImg: function (e) {
    var id = e.currentTarget.dataset.id;
    var typ = 'ban';
    var data = {
      id: id,
      typ: typ,
    }
    ikz.nav('../../Home/images/images', 'n', data);
  },
  //选择分类
  chooseType: function (e) {
    var datyp = e.currentTarget.dataset.datyp;
    var that = this;
    var page = 1;
    var limit = that.data.limit;
  if (datyp == 'yesterday') {
    var bg_today = '#fff';
      var co_today = '#000';
      var bg_yesterday = 'linear-gradient(-135deg, #0AC4BA 0%, #2BDA8E 100%)';
      var co_yesterday = '#fff';
      var bg_all = '';
      var co_all = '';

    } else {
    var bg_today = 'linear-gradient(-135deg, #0AC4BA 0%, #2BDA8E 100%)';
    var co_today = '#fff';
    var bg_yesterday = '#fff';
    var co_yesterday = '#000';
    var bg_all = '';
    var co_all = '';
    };
    var data = {
      page: page,
      limit: limit,
      datyp: datyp,
    }
    var path = 'Conven/indexfen';
    ikz.request(path, data, false, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        if (res.data.conven_info.length) {
          for (var i = 0; i < res.data.conven_info.length; i++) {
            res.data.conven_info[i].conven_addtime = that.diaplayTime(res.data.conven_info[i].conven_addtime);
            if (res.data.conven_info[i].conven_top == 2) {
              arrs.push(res.data.conven_info)
            }
          }
        }
        that.setData({
          conven_info: res.data.conven_info,
          bg_today: bg_today,//今日信息背景色
          co_today: co_today,//今日信息字体色
          bg_yesterday: bg_yesterday,//昨日信息背景色
          co_yesterday: co_yesterday,//昨日信息字体色
          bg_all: bg_all,//全部信息背景色
          co_all: co_all,//全部信息字体色
          datyp: datyp,
        })
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });

  },
  //拨打电话
  toPhone: function (e) {
    var phone = this.data.shop_info.self_phone;
    wx.makePhoneCall({
      phoneNumber: phone,
    })

  },
  // 置顶
  toTop: function () {
    wx.pageScrollTo({
      scrollTop: 0
    })
  },

  //跳转到优选页
  toPrefe: function (e) {
    ikz.nav('../prefelist/prefelist', 'n')
  },

  //跳转到详情页
  toContent: function (e) {
    var cid = e.currentTarget.dataset.cid;
    var data = {
      cid: cid,
    }
    ikz.nav('../content/content', 'n', data);
  },

  //分类列表
  chooseSon: function (e) {
    var tid = e.currentTarget.dataset.tid;
    var data = {
      tid: tid,
    }
    ikz.nav('../typelist/typelist', 'n', data);
  },
  //时间转换
  diaplayTime(data) {
    var str = data;
    //将字符串转换成时间格式
    var timePublish = new Date(str);
    //现在时间
    var timeNow = new Date();
    //当天0点时间
    var timeStart = new Date(new Date(new Date().toLocaleDateString()).getTime());
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var diffValue = timeNow - timePublish;
    var diffHour = diffValue / hour;
    var diffMinute = diffValue / minute;

    if (diffValue < 0) {
      wx.showToast({
        title: '时间错误！',
        duration: 2000
      });
      return false;
    } else {
      //判断是今天
      if (timePublish >= timeStart) {
        if (diffHour > 1) {
          var result = parseInt(diffHour) + "小时前";
        } else if (diffMinute > 1) {
          var result = parseInt(diffMinute) + "分钟前";
        } else {
          var result = "刚刚发表";
        }
      } else {//判断不是今天
        var result = str;
      }
    }
    return result;
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    // 页面初始化 options为页面跳转所带来的参数

    ikz.over(this);
    var that = this;
    var page = 1;
    var limit = that.data.limit;

    var datyp = 'today';

    console.log(options)
    console.log(33333)
  if (datyp == 'yesterday') {
    var bg_today = '#fff';
    var co_today = '#000';
    var bg_yesterday = 'linear-gradient(-135deg, #0AC4BA 0%, #2BDA8E 100%)';
    var co_yesterday = '#fff';
    var bg_all = '';
    var co_all = '';

    } else {
    var bg_today = 'linear-gradient(-135deg, #0AC4BA 0%, #2BDA8E 100%)';
    var co_today = '#fff';
    var bg_yesterday = '#fff';
    var co_yesterday = '#000';
    var bg_all = '';
    var co_all = '';
    };
    var data = {
      page: page,
      limit: limit,
      datyp: datyp,
    };
    var path = 'Conven/index';
    ikz.request(path, data, false, (res) => {
      console.log(res.data)
      if (res.data.conven_info.length) {
        for (var i = 0; i < res.data.conven_info.length; i++) {
          res.data.conven_info[i].conven_addtime = that.diaplayTime(res.data.conven_info[i].conven_addtime);
          if (res.data.conven_info[i].conven_top == 2) {
            arrs.push(res.data.conven_info)
          }
        }
      }
      if (res.data.status == 'success') {
        var all_browse = res.data.shop_info.self_all_browse;
        var all_release = res.data.all_release;

        that.setData({
          banner_info: res.data.banner_info,
          conven_type_info: res.data.conven_type_info,
          all_browse: all_browse,
          all_release: all_release,
          conven_info: res.data.conven_info,
          shop_info: res.data.shop_info,
          conven_top_info: res.data.conven_top_info,
          page: page,
          bg_today: bg_today,//今日信息背景色
          co_today: co_today,//今日信息字体色
          bg_yesterday: bg_yesterday,//昨日信息背景色
          co_yesterday: co_yesterday,//昨日信息字体色
          bg_all: bg_all,//全部信息背景色
          co_all: co_all,//全部信息字体色
          datyp: datyp,
          prodis: 'none',
        });
        ikz.hide(that);
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  tap_ch: function (e) {//点击的时候设置这个属性的值
    if (this.data.open) {
      this.setData({
        open: false//之前是打开的话现在设置为关闭
      });
    } else {
      this.setData({
        open: true
      });
    }
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    var that = this;
    var userInfo = app.globalData.user_openinfo;
    that.setData({
      userInfo: userInfo,
      islog: 2,
    })
    if (app.globalData.openUserInfo == '') {
      that.setData({
        disbtn: 'none',
      })
    } else {

      var path = 'Percenter/service';
      ikz.request(path, {}, true, (res) => {
        console.log(res);
        if (res.data.status == 'success') {

          that.setData({
            shop_info: res.data.shop_info,
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '服务器繁忙，请稍后重试！',
            showCancel: false
          })
        }

      });
      that.setData({
        disbtn: 'block',
      })
    }
    ikz.hide(that);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showLoading({
      title: '玩命加载中',
    })
    var that = this;
    var page = that.data.page + 1;
    var old_info = that.data.conven_info;
    that.setData({
      page: page,
    })
    var limit = that.data.limit;
    var datyp = that.data.datyp;
    var data = {
      page: page,
      limit: limit,
      datyp: datyp,
    }
    var path = 'Conven/indexfen';
    ikz.request(path, data, false, (res) => {
      console.log(res);
      if (res.data.status == 'success') {
        var new_info = res.data.conven_info;
        if (new_info.length) {
          if (new_info.length) {
            for (var i = 0; i < new_info.length; i++) {
              new_info[i].conven_addtime = that.diaplayTime(new_info[i].conven_addtime);
              if (new_info[i].conven_top == 2) {
                arrs.push(new_info)
              }
            }
          }
          var all_info = old_info.concat(new_info);
          var prodis = 'none';
        } else {
          var all_info = old_info;
          var prodis = 'block';
        }
        that.setData({
          conven_info: all_info,
          prodis: prodis,
        })
        // 隐藏导航栏加载框  
        wx.hideNavigationBarLoading();
        // 停止下拉动作  
        wx.stopPullDownRefresh();
        wx.hideLoading()
      } else {
        wx.showModal({
          title: '提示',
          content: '服务器繁忙，请稍后重试！',
          showCancel: false
        })
      }

    });
    wx.hideLoading();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})